﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Admin_BouCatalog : System.Web.UI.Page
{
    CatalogBUS caBUS = new CatalogBUS();
    DataTable tbCatalog = new DataTable();
    DataView viewCatalog;
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
        lblError.Text = "";
    }
    private void LoadData()
    {
        tbCatalog = caBUS.returnAllCatalog();
        viewCatalog = new DataView(tbCatalog);
        viewCatalog.RowFilter = "C_Status=1";
        grvBouCatalog.DataSource = viewCatalog;
        grvBouCatalog.DataBind();
    }
    protected void grvBouCatalog_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvBouCatalog.PageIndex = e.NewPageIndex;
        LoadData();
    }

    protected void grvBouCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Remove"))
        {
            bool check = caBUS.updateCatalogStatus(Int32.Parse(e.CommandArgument.ToString()));
            if (check)
                LoadData();
            txtValue.Text = "";
            lblID.Text = "";

        }
        if (e.CommandName.Equals("Up"))
        {

            viewCatalog.RowFilter = "C_ID=" + e.CommandArgument.ToString();
            txtValue.Text = viewCatalog[0]["C_Name"].ToString();
            lblID.Text = e.CommandArgument.ToString();
        }
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        if (txtValue.Text.Equals(""))
        {
            lblError.Text = "You must enter content";
        }
        else
        {
            bool i = caBUS.insertCatalog(txtValue.Text);
            if (i)
            {
                LoadData();
                lblError.Text = "Insert New Catalog Success";
            }
            else
            {
                lblError.Text = "Insert New Catalog Error !";
            }
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            if (txtValue.Text.Equals(""))
            {
                lblError.Text = "You must enter content";
            }
            else
            {
                bool i = caBUS.updateCatalog(Int32.Parse(lblID.Text), txtValue.Text);
                if (i)
                {
                    LoadData();
                    lblError.Text = "Edit Catalog Success";
                }
                else
                {
                    lblError.Text = "Edit Catalog Error !";
                }
            }
        }catch(Exception ex){
            lblError.Text = "You must choice bouquet catalog";
        }
    }
}